Imports SpringProxAPI

Public Class ShowUidMain
    Inherits System.Windows.Forms.Form

    Dim ReaderActive As Boolean
    Dim ReaderError As Boolean
    Dim LastTagFound As String
    Dim LastTagType As String

    Friend WithEvents lbStatus As System.Windows.Forms.Label
    Friend WithEvents eStatus As System.Windows.Forms.TextBox
    Friend WithEvents lbSnr As System.Windows.Forms.Label
    Friend WithEvents eSnr As System.Windows.Forms.TextBox
    Friend WithEvents eAts As System.Windows.Forms.TextBox
    Friend WithEvents lbAts As System.Windows.Forms.Label
    Friend WithEvents tmrWork As System.Windows.Forms.Timer
    Friend WithEvents lbDesc As System.Windows.Forms.Label
    Friend WithEvents eInfo As System.Windows.Forms.TextBox
    Private WithEvents header As System.Windows.Forms.PictureBox
    Private WithEvents branded As System.Windows.Forms.PictureBox
    Private WithEvents background As System.Windows.Forms.PictureBox
    Friend WithEvents lbInfo As System.Windows.Forms.Label


    Public Sub New()
        MyBase.New()
        InitializeComponent()
        ReaderActive = False
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        MyBase.Dispose(disposing)
    End Sub

    Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(ShowUidMain))
        Me.lbStatus = New System.Windows.Forms.Label
        Me.eStatus = New System.Windows.Forms.TextBox
        Me.lbSnr = New System.Windows.Forms.Label
        Me.eSnr = New System.Windows.Forms.TextBox
        Me.eAts = New System.Windows.Forms.TextBox
        Me.lbAts = New System.Windows.Forms.Label
        Me.tmrWork = New System.Windows.Forms.Timer
        Me.lbDesc = New System.Windows.Forms.Label
        Me.eInfo = New System.Windows.Forms.TextBox
        Me.lbInfo = New System.Windows.Forms.Label
        Me.header = New System.Windows.Forms.PictureBox
        Me.branded = New System.Windows.Forms.PictureBox
        Me.background = New System.Windows.Forms.PictureBox
        '
        'lbStatus
        '
        Me.lbStatus.Location = New System.Drawing.Point(8, 32)
        Me.lbStatus.Size = New System.Drawing.Size(100, 20)
        Me.lbStatus.Text = "Status :"
        '
        'eStatus
        '
        Me.eStatus.Location = New System.Drawing.Point(8, 48)
        Me.eStatus.ReadOnly = True
        Me.eStatus.Size = New System.Drawing.Size(216, 21)
        '
        'lbSnr
        '
        Me.lbSnr.Location = New System.Drawing.Point(8, 80)
        Me.lbSnr.Size = New System.Drawing.Size(100, 16)
        Me.lbSnr.Text = "//"
        Me.lbSnr.Visible = False
        '
        'eSnr
        '
        Me.eSnr.Location = New System.Drawing.Point(8, 96)
        Me.eSnr.ReadOnly = True
        Me.eSnr.Size = New System.Drawing.Size(216, 21)
        Me.eSnr.Visible = False
        '
        'eAts
        '
        Me.eAts.Location = New System.Drawing.Point(8, 184)
        Me.eAts.ReadOnly = True
        Me.eAts.Size = New System.Drawing.Size(216, 21)
        Me.eAts.Visible = False
        '
        'lbAts
        '
        Me.lbAts.Location = New System.Drawing.Point(8, 168)
        Me.lbAts.Size = New System.Drawing.Size(100, 16)
        Me.lbAts.Text = "//"
        Me.lbAts.Visible = False
        '
        'tmrWork
        '
        Me.tmrWork.Enabled = True
        '
        'lbDesc
        '
        Me.lbDesc.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold)
        Me.lbDesc.ForeColor = System.Drawing.Color.FromArgb(CType(CType(217, Byte), Integer), CType(CType(9, Byte), Integer), CType(CType(27, Byte), Integer))
        Me.lbDesc.Location = New System.Drawing.Point(50, 218)
        Me.lbDesc.Size = New System.Drawing.Size(133, 16)
        Me.lbDesc.Text = "SpringProx SDK sample"
        Me.lbDesc.TextAlign = System.Drawing.ContentAlignment.TopCenter
        '
        'eInfo
        '
        Me.eInfo.Location = New System.Drawing.Point(8, 136)
        Me.eInfo.ReadOnly = True
        Me.eInfo.Size = New System.Drawing.Size(216, 21)
        Me.eInfo.Visible = False
        '
        'lbInfo
        '
        Me.lbInfo.Location = New System.Drawing.Point(8, 120)
        Me.lbInfo.Size = New System.Drawing.Size(100, 16)
        Me.lbInfo.Text = "//"
        Me.lbInfo.Visible = False
        '
        'header
        '
        Me.header.Image = CType(resources.GetObject("header.Image"), System.Drawing.Image)
        Me.header.Location = New System.Drawing.Point(0, 0)
        Me.header.Size = New System.Drawing.Size(240, 30)
        '
        'branded
        '
        Me.branded.Image = CType(resources.GetObject("branded.Image"), System.Drawing.Image)
        Me.branded.Location = New System.Drawing.Point(173, 243)
        Me.branded.Size = New System.Drawing.Size(67, 30)
        '
        'background
        '
        Me.background.Image = CType(resources.GetObject("background.Image"), System.Drawing.Image)
        Me.background.Location = New System.Drawing.Point(0, 21)
        Me.background.Size = New System.Drawing.Size(240, 252)
        '
        'ShowUidMain
        '
        Me.BackColor = System.Drawing.Color.White
        Me.ClientSize = New System.Drawing.Size(240, 274)
        Me.Controls.Add(Me.branded)
        Me.Controls.Add(Me.header)
        Me.Controls.Add(Me.lbInfo)
        Me.Controls.Add(Me.eInfo)
        Me.Controls.Add(Me.lbDesc)
        Me.Controls.Add(Me.lbAts)
        Me.Controls.Add(Me.eAts)
        Me.Controls.Add(Me.eSnr)
        Me.Controls.Add(Me.lbSnr)
        Me.Controls.Add(Me.eStatus)
        Me.Controls.Add(Me.lbStatus)
        Me.Controls.Add(Me.background)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Text = "ShowUID"

    End Sub

    Public Sub Lookup14443A()

        Dim rc As Short
        Dim i As Integer
        Dim s As String

        Dim cid As Byte
        cid = 0

        Dim atq As Byte()
        Dim snr As Byte()
        Dim sak As Byte()
        Dim ats As Byte()

        Dim snrlen As Byte
        snrlen = 12

        Dim atslen As Byte
        atslen = 32

        ReDim atq(2)
        ReDim snr(12)
        ReDim sak(1)
        ReDim ats(32)

        rc = SPROX.TclA_ActivateAny(atq, snr, snrlen, sak)

        If (rc = SPROX.TCL_NOTAGERR) Or (rc = SPROX.MI_OK) Then
            'Tag found, check its serial number
            s = ""
            For i = 0 To snrlen - 1
                s += String.Format("{0:X02}", snr(i))
            Next
            If s = LastTagFound Then
                Exit Sub
            End If
            LastTagType = "A"
            LastTagFound = s

            'OK, display the info
            eStatus.Text = "ISO/IEC 14443-A card found"
            lbSnr.Text = "UID"
            eSnr.Text = s
            lbInfo.Text = "ATQa and SAK"
            eInfo.Text = String.Format("ATQ={0:X02}{1:X02} SAK={2:X02}", atq(1), atq(0), sak(0))

            If rc = SPROX.MI_OK Then
                ' This is a T=CL tag, let's retrieve its ATS
                rc = SPROX.TclA_GetAts(cid, ats, atslen)
                If rc = SPROX.MI_OK Then
                    s = ""
                    For i = 0 To atslen - 1
                        s += String.Format("{0:X02}", ats(i))
                    Next
                    lbAts.Text = "ATS"
                    eAts.Text = s
                End If
            Else
                ' This is only a layer 3 tag
                lbAts.Text = "Not T=CL"
                eAts.Text = ""
            End If

        ElseIf rc = SPROX.MI_NOTAGERR Then
            ' No tag on the reader
            LastTagFound = ""
            LastTagType = ""
        Else
            'Error (maybe not fatal, but we keep it simple...)
            ReaderError = True
        End If
    End Sub

    Public Sub Lookup14443B()

        Dim rc As Short
        Dim i As Integer
        Dim s As String

        Dim cid As Byte
        cid = 0

        Dim afi As Byte
        afi = 0

        Dim atq As Byte()
        Dim ats As Byte()

        Dim atslen As Byte
        atslen = 32

        ReDim atq(11)
        ReDim ats(32)

        rc = SPROX.TclB_ActivateAny(afi, atq)

        If rc = SPROX.TCL_NOTAGERR Or rc = SPROX.MI_OK Then
            'Tag found, check its serial number
            s = ""
            For i = 0 To 4
                s += String.Format("{0:X02}", atq(i))
            Next
            If s = LastTagFound Then
                Exit Sub
            End If
            LastTagType = "B"
            LastTagFound = s

            'OK, display the info
            eStatus.Text = "ISO/IEC 14443-B card found"
            lbSnr.Text = "PUPI"
            eSnr.Text = s
            For i = 4 To 11
                s += String.Format("{0:X02}", atq(i))
            Next
            lbInfo.Text = "Full ATQb"
            eInfo.Text = s

            If rc = SPROX.MI_OK Then
                ' This is a T=CL tag, let's retrieve its ATS
                rc = SPROX.TclB_Attrib(atq, cid, ats, atslen)
                If rc = SPROX.MI_OK Then
                    s = ""
                    For i = 0 To atslen - 1
                        s += String.Format("{0:X02}", ats(i))
                    Next
                    lbAts.Text = "ATS"
                    eAts.Text = s
                End If
            Else
                ' This is only a layer 3 tag
                lbAts.Text = "Not T=CL"
                eAts.Text = ""
            End If


        ElseIf rc = SPROX.MI_NOTAGERR Then
            ' No tag on the reader
            LastTagFound = ""
            LastTagType = ""
        Else
            'Error (maybe not fatal, but we keep it simple...)
            ReaderError = True
        End If

    End Sub

    Public Sub FindCard()

        Dim rc As Short
        Dim i As Integer
        Dim s As String
        Dim card_proto As System.UInt16
        Dim want_protos As Integer = &HFFFF

        Dim uid As Byte()
        Dim uidlen As Byte
        uidlen = 12

        ReDim uid(12)
        
        rc = SPROX.Find(System.Convert.ToUInt16(want_protos), card_proto, uid, uidlen)

        If rc = SPROX.MI_OK Then
            SPROX.ControlBuzzer(UInt16.Parse("100"))

            'Tag found, check its serial number
            s = ""
            For i = 0 To (uidlen - 1)
                s += String.Format("{0:X02}", uid(i))
            Next
            If s = LastTagFound Then
                Exit Sub
            End If

            LastTagType = "X"

            'OK, display the info
            Select Case (System.Convert.ToInt16(card_proto))
                Case 1 'SPROX.PROTO_14443_A
                    eStatus.Text = "ISO/IEC 14443-A card found"
                    lbSnr.Text = "UID"
                    LastTagType = "A"

                Case 2 'SPROX.PROTO_14443_B
                    eStatus.Text = "ISO/IEC 14443-B card found"
                    lbSnr.Text = "PUPI"
                    LastTagType = "B"

                Case 128 'SPROX.PROTO_14443_Bi
                    eStatus.Text = "Calypso card found (Innovatron)"
                    lbSnr.Text = "SNR"

                Case 4 'SPROX.PROTO_15693
                    eStatus.Text = "ISO/IEC 15693 card found"
                    lbSnr.Text = "UID"

                Case 8 'SPROX.PROTO_ICODE1
                    eStatus.Text = "NXP ICODE-1 card found"
                    lbSnr.Text = "SNR"

                Case 64 'SPROX.PROTO_MEM_ASK
                    eStatus.Text = "ASK CTS256B/CTS512B card found"
                    lbSnr.Text = "SNR"

                Case 32 'SPROX.PROTO_MEM_ST
                    eStatus.Text = "S.T. MicroElectronics card found"
                    lbSnr.Text = "UID"

                Case 16 'PROTO_MEM_PICO
                    eStatus.Text = "Inside Contactless card found"
                    lbSnr.Text = "SNR"

            End Select

            If LastTagType = "A" Then
                Lookup14443A()
            ElseIf LastTagType = "B" Then
                Lookup14443B()
            Else
                LastTagFound = s
                eSnr.Text = s
            End If

        ElseIf rc = SPROX.MI_NOTAGERR Then
            ' No tag on the reader
            LastTagFound = ""
            LastTagType = ""
        Else
            'Error (maybe not fatal, but we keep it simple...)
            ReaderError = True
        End If

    End Sub

    Public Sub Polling()

        SPROX.ControlRF(1)

        If LastTagFound <> "" Then
            SPROX.ControlLed(1, 0)
        End If

        FindCard()

        SPROX.ControlRF(0)

        If LastTagFound = "" Then
            SPROX.ControlLed(0, 0)
            lbSnr.Visible = False
            eSnr.Visible = False
            lbInfo.Visible = False
            eInfo.Visible = False
            lbAts.Visible = False
            eAts.Visible = False
            eStatus.Text = "No card on the reader"
        Else
            SPROX.ControlLed(0, 1)
            lbSnr.Visible = True
            eSnr.Visible = True
            If LastTagType = "A" Or LastTagType = "B" Then
                lbInfo.Visible = True
                eInfo.Visible = True
                lbAts.Visible = True
                eAts.Visible = True
            Else
                lbInfo.Visible = False
                eInfo.Visible = False
                lbAts.Visible = False
                eAts.Visible = False
            End If

        End If


    End Sub

    Private Sub tmrWork_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrWork.Tick

        tmrWork.Enabled = False

        ReaderError = False

        If Not ReaderActive Then
            Dim rc As Short
            rc = SPROX.ReaderOpen("") '"COM3:"
            If rc = SPROX.MI_OK Then
                ReaderActive = True
            End If
        Else
            Polling()
        End If

        If ReaderError Then
            ReaderActive = False
            SPROX.ReaderClose()
        End If

        If LastTagFound = "" Then
            tmrWork.Interval = 100
        Else
            tmrWork.Interval = 1000
        End If

        tmrWork.Enabled = True
    End Sub
End Class
